package com.dldata.display.control.System;

import com.dldata.display.entity.SysHospitalBranchKsEntity;
import com.dldata.display.entity.SysKsEntity;
import com.dldata.display.service.System.SysHospitalBranchKsService;
import com.dldata.display.service.System.SysKeShiService;
import com.dldata.display.utils.StringUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by 倪继文 on 2017/6/19.
 */

@Controller
@RequestMapping(value = "/SysHospitalBranchKs")
public class SysHospitalBranchKsController {


    @Autowired
    SysHospitalBranchKsService hospitalBranchKsService;
    @Autowired
    SysKeShiService sysKeShiService;


//    @RequestMapping("/saveHospitalBranchKs")
//    public void saveHospitalBranchKs(String ksids,String yqId, HttpServletRequest request
//            , HttpServletResponse response)throws Exception{
//
//        String result;
//        String[] ids = ksids.split(",");
//
//        List<SysHospitalBranchKsEntity> sysHospitalBranchKsEntityList = new ArrayList<SysHospitalBranchKsEntity>();
//
//
//        for(int i=0;i<ids.length;i++){
//            SysHospitalBranchKsEntity t = new SysHospitalBranchKsEntity();
//            t.setId(UUID.randomUUID().toString());
//            t.setKsid(ids[i]);
//            t.setYqid(yqId);
//
//            sysHospitalBranchKsEntityList.add(t);
//        }
//
//        try{
//            hospitalBranchKsService.save(sysHospitalBranchKsEntityList);
//            result = "{\"success\":true,\"msg\":\"保存成功！\"}";
//        }
//        catch (Exception ex){
//            result = "{\"success\":false,\"msg\":\"系统错误，请稍候再试！\"}";
//        }
//
//        StringUtil.writeToWeb(result, "html", response);
//
//    }


    @RequestMapping("/getKsByYqId")
    @ResponseBody
    public String getKsByYqId(String yqid){

        List<SysKsEntity> sysKeShi = sysKeShiService.getAllKeShi();
        List<Map<String, String>> listmap = new ArrayList<Map<String, String>>();
        List<SysHospitalBranchKsEntity>  sysHospitalBranchKsEntities = hospitalBranchKsService.getKsByYqId(yqid);

        try{
            sysHospitalBranchKsEntities.forEach(s->{
                Map<String, String> map = new HashMap<String, String>();
                map.put("id",(s.getId()));

                sysKeShi.stream().filter(t->t.getId().equals(s.getKsid())).limit(1).forEach(t ->{
                    map.put("code",t.getCode()==null?"":t.getCode());
                    map.put("name",t.getName()==null?"":t.getName());
                });
                listmap.add(map);
            });
        }
        catch (Exception ex){

        }
        return listmap.size()>0 ? JSONArray.fromObject(listmap).toString() : "[]";
    }



    @RequestMapping("/getNoKsByYqId")
    @ResponseBody
    public String getNoKsByYqId(String yqid){

        List<SysKsEntity> sysKeShi = sysKeShiService.getAllKeShi();
        List<Map<String, String>> listmap = new ArrayList<Map<String, String>>();
        List<SysHospitalBranchKsEntity>  sysHospitalBranchKsEntities = hospitalBranchKsService.getKsByYqId(yqid);

        try{
            sysKeShi.forEach(s->{
                Map<String, String> map = new HashMap<String, String>();
                map.put("id",(s.getId()));

                List<SysHospitalBranchKsEntity> ts = sysHospitalBranchKsEntities.stream().filter(t->t.getKsid().equals(s.getId())).collect(Collectors.toList());
                if(ts.size()==0) {
                    map.put("code",s.getCode()==null?"":s.getCode());
                    map.put("name",s.getName()==null?"":s.getName());
                    listmap.add(map);
                }
            });
        }
        catch (Exception ex){

        }
        return listmap.size()>0 ? JSONArray.fromObject(listmap).toString() : "[]";
    }




    @RequestMapping("/delete")
    public void delete(String id, HttpServletRequest request
            , HttpServletResponse response)throws Exception{

        hospitalBranchKsService.delete(id);
        String result = "{\"success\":true,\"msg\":\"保存成功！\"}";

        StringUtil.writeToWeb(result, "html", response);
    }


    @RequestMapping("/addyqks")
    public void addyqks(String ksid,String yqId, HttpServletRequest request
            , HttpServletResponse response)throws Exception{

        SysHospitalBranchKsEntity t = new SysHospitalBranchKsEntity();
        t.setId(UUID.randomUUID().toString());
        t.setKsid(ksid);
        t.setYqid(yqId);


        hospitalBranchKsService.save(t);
        String result = "{\"success\":true,\"msg\":\"保存成功！\"}";
        StringUtil.writeToWeb(result, "html", response);


    }
}
